package com.ruiyin.common;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.CaseInsensitiveContainerFactory;
import com.jfinal.plugin.activerecord.dialect.OracleDialect;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.render.ViewType;
import com.jfinal.weixin.demo.WeixinApiController;
import com.jfinal.weixin.demo.WeixinMsgController;
import com.jfinal.weixin.demo.WeixinPayController;
import com.jfinal.weixin.sdk.api.ApiConfigKit;
import com.ruiyin.bean.Microchannelinformation;
import com.ruiyin.bean.Microchannelstate;
import com.ruiyin.bean.Organ;
import com.ruiyin.bean.SysUser;
import com.ruiyin.bean.Weixinparameters;
import com.ruiyin.controller.IndexController;
import com.ruiyin.controller.LoginController;
import com.ruiyin.controller.UserController;
import com.ruiyin.controller.WeixinApiControlller;
import com.ruiyin.controller.WeixinController;
import com.ruiyin.controller.WeixinMsgControlle;
import com.ruiyin.httpinte.HttpInterFaceController;

public class Config extends JFinalConfig  {

	/**
	 * 配置常量
	 */
	public void configConstant(Constants me) {
		// 加载少量必要配置，随后可用PropKit.get(...)获取值
		PropKit.use("a_little_config.txt");
		me.setDevMode(PropKit.getBoolean("devMode", false));
		me.setEncoding("utf-8");
		me.setViewType(ViewType.JSP);
		me.setBaseViewPath("/WEB-INF/jsp");//默认视图路径
		//com.jfinal.render.CPI.setJspRenderSupportActiveRecord(false);
		
		//me.setBaseDownloadPath(baseDownloadPath);//默认下载路径
		//me.setBaseUploadPath(baseUploadPath);//默认上传路径
		ApiConfigKit.setDevMode(me.getDevMode());
	}
	
	/**
	 * 配置路由
	 */
	public void configRoute(Routes me) {
		me.add("/", IndexController.class);	
		me.add("/login", LoginController.class);	// 第三个参数为该Controller的视图存放路径
		me.add("/user",UserController.class);
		me.add("/auth",WeixinController.class,"/weixin");
		me.add("/msg", WeixinMsgController.class);
		me.add("/msgs", WeixinMsgControlle.class);
		me.add("/api", WeixinApiController.class, "/api");
		me.add("/apis", WeixinApiControlller.class, "/apis");
		me.add("/pay", WeixinPayController.class);
		me.add("/httpinterface", HttpInterFaceController.class);
		//me.add("/loginajax", LoginController.class,"/index1.jsp");	// 第三个参数为该Controller的视图存放路径
	//	me.add("/blog", BlogController.class);			// 第三个参数省略时默认与第一个参数值相同，在此即为 "/blog"
	}
	
	public static C3p0Plugin createC3p0Plugin() {
		return new C3p0Plugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password").trim());
	}
	
	/**
	 * 阿里巴巴连接池
	 * @return
	 */
	public static DruidPlugin createDruidPlugin() {
		   DruidPlugin dp=new DruidPlugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password").trim(), PropKit.get("driverClassName").trim());
		    dp.setValidationQuery("select 1 from dual");
		    return dp;
	}
	
	/**
	 * 配置插件
	 */
	public void configPlugin(Plugins me) {
		// 配置C3p0数据库连接池插件
	//	C3p0Plugin C3p0Plugin = createC3p0Plugin();
		//me.add(C3p0Plugin);
		//配置阿里巴巴连接池
		DruidPlugin dp=createDruidPlugin();
		
	    me.add(dp);
		// 配置ActiveRecord插件
		ActiveRecordPlugin arp = null;
	    arp=new ActiveRecordPlugin(dp);//设置数据库方言
	    arp.setDialect(new OracleDialect());
	    arp.setContainerFactory(new CaseInsensitiveContainerFactory());//忽略大小写
	  //  me.add(new EhCachePlugin());
	    arp.setShowSql(true);//这个必须有的
	    
	    //类和数据库表的映射 start
	    //三个参数：表名、主键名、对应的实体类名
	    arp.addMapping("sys_user","USER_ID",SysUser.class);
	    arp.addMapping("organ", Organ.class);
	    arp.addMapping("weixinparameters", Weixinparameters.class);
		arp.addMapping("MICROCHANNELINFORMATION","OPENID",Microchannelinformation.class);
		arp.addMapping("MICROCHANNELSTATE","OPENID",Microchannelstate.class);
	    //类和数据库表的映射 start  end
	    
	    //自动注册 不怎么好用 感兴趣可以研究下
	    /*
		AutoTableBindPlugin atb = new AutoTableBindPlugin(dp);
		atb.setDialect(new OracleDialect());
		atb.setContainerFactory(new CaseInsensitiveContainerFactory());//忽略大小写
		atb.setShowSql(true);//这个必须有的
		*/
		me.add(arp);
		
		// 所有配置在 MappingKit 中搞定
		//_MappingKit.mapping(arp);
	}
	
	/**
	 * 配置全局拦截器
	 */
	public void configInterceptor(Interceptors me) {
	
	}
	
	/**
	 * 配置处理器
	 */
	public void configHandler(Handlers me) {
	
	}
	/**
	 * 建议使用 JFinal 手册推荐的方式启动项目
	 * 运行此 main 方法可以启动项目，此main方法可以放置在任意的Class类定义中，不一定要放于此
	 */
	public static void main(String[] args) {
		JFinal.start("WebRoot", 80, "/", 5);
	}

}
